﻿using M_Purchase.Api.Appliction.Command.PurchCommand;
using M_Purchase.Domain;
using M_Purchase.Repository.PurchaseInterface;
using MediatR;

namespace M_Purchase.Api.Appliction.CommandHandlers.PurchCommandHandler
{
    /// <summary>
    /// 选择供应商
    /// </summary>
    public class SupplierUpDateIDCommandHandler : IRequestHandler<SupplierUpDateIDCommand, int>
    {
        /// <summary>
        /// 采购仓储
        /// </summary>
        private readonly IPurchRepository _purchRepository;
        /// <summary>
        /// 供应商仓储
        /// </summary>
        private readonly ISupplierRepository _supplierRepository;
        private readonly ILogisticsRepository _logisticsRepository;
        public SupplierUpDateIDCommandHandler(IPurchRepository purchRepository,ISupplierRepository supplierRepository, ILogisticsRepository logisticsRepository)
        {
            _purchRepository = purchRepository;
            _supplierRepository = supplierRepository;
            _logisticsRepository = logisticsRepository;
        }

        public async Task<int> Handle(SupplierUpDateIDCommand request, CancellationToken cancellationToken)
        {
          
          var purchR = await _purchRepository.GetModelsAsync(request.PurchaseID);
            purchR.SupplierID = request.SupplierID;
            purchR.PurchaseSart = "已采购";
                purchR.PurchasePrice = 15;
             
                purchR.PurchaseSumMoney = purchR.PurchasePrice * purchR.PurchaseNumber;
            purchR.PayType = "定金+尾款";
                purchR.PurchasePayPrice = "定金" + ((int)purchR.PurchaseSumMoney * 0.1)+"元" +"+"+ "尾款" + ((int)purchR.PurchaseSumMoney * 0.9)+"元";
                 await _purchRepository.UpdateAsync(purchR);
         
                await _logisticsRepository.AddAsync(new Logistics()
                {
                    
                    PurchaseID= request.PurchaseID,
                    SupplierID=request.SupplierID,
                    LogisticsCode = DateTime.Now.ToString("yyyyMMddHHmmss"),
                    PayTime = DateTime.Now,
                    Sarte="待发货"

                });
            
            return 1;
          
           


        }
    }
}
